<?php
/**
 * User: xuhong
 * Date: 2018/10/30
 */

namespace extend\upload;

use think\facade\Filesystem;

class Upload
{
    public function file($file, $option)
    {
        //判断文件是否上传成功
        if (!empty($file)) {
            if ($option['replace']) {
                $filePath = Filesystem::disk('uploads')->putFile($option['path'], $file);
            } else {
                $filePath = Filesystem::disk('uploads')->putFile($option['path'], $file);
            }
            $filePath = '/uploads/' . $filePath;
            if ($filePath) {
                $option['type'] = explode('/', mime_content_type('.' . $filePath))[0];
                return ['path' => $filePath, 'url' => config('app.upload_host') . $filePath, 'type' => $option['type']];
            } else {
                return $file->getError();
            }
        } else {
            return '非法文件错误';
        }
    }
}